package com.lc.zpyh.ui.fragment.mine;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.zpyh.R;
import com.lc.zpyh.app.AppFragment;
import com.lc.zpyh.http.request.UserCouponQueryApi;
import com.lc.zpyh.http.response.UserCouponQueryBean;
import com.lc.zpyh.other.IntentKey;
import com.lc.zpyh.ui.activity.home.SelectMerchantsFlActivity;
import com.lc.zpyh.ui.activity.mine.CouponActivity;
import com.lc.zpyh.ui.activity.supermarket.BusinessActivity;
import com.lc.zpyh.util.BigDecimalUtils;
import com.lc.zpyh.util.SPUtil;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class CouponListFragment extends AppFragment<CouponActivity> {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    // TODO: Rename and change types of parameters
    private String system_id;

    private BaseQuickAdapter<UserCouponQueryBean.ListBean, BaseViewHolder> adapter;

    public static CouponListFragment newInstance(String system_id) {
        CouponListFragment fragment = new CouponListFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, system_id);

        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            system_id = getArguments().getString(ARG_PARAM1);

        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        userCouponQuery(system_id);


    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_coupon_list;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    /**
     * 优惠券
     */
    private void userCouponQuery(String type) {
        EasyHttp.post(this)
                .api(new UserCouponQueryApi().setUserId(SPUtil.get(getContext(), IntentKey.USERID, "")).setType(type).setPageNum("1").setPageSize("100")).request(new HttpCallback<UserCouponQueryBean>(this) {

            @Override
            public void onSucceed(UserCouponQueryBean data) {
                if (data.getCode()==0) {
                    adapter = new BaseQuickAdapter<UserCouponQueryBean.ListBean, BaseViewHolder>
                            (R.layout.item_coupon,data.getList()) {

                        @Override
                        protected void convert(BaseViewHolder baseViewHolder,UserCouponQueryBean.ListBean dataBean) {
                            baseViewHolder.setVisible(R.id.ll_to_make,true);
                            baseViewHolder.setText(R.id.tv_address,dataBean.getName());
                            baseViewHolder.setText(R.id.tv_money, BigDecimalUtils.div(dataBean.getCreditFor()+"","100",2));

                            baseViewHolder.setText(R.id.tv_date, Html.fromHtml("满"+BigDecimalUtils.div(dataBean.getMeetTheConditions()+"","100",0)+"可用 <br>"+dataBean.getCouponStartTime()+"-"+dataBean.getCouponOverdueTime()+"<br/>"));
                        baseViewHolder.getView(R.id.ll_to_make).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (dataBean.getCouponsType()==0){
                                    startActivity(new Intent(getContext(), BusinessActivity.class).putExtra("id",dataBean.getId()+""));
                                }else {
                                    startActivity(new Intent(getContext(), SelectMerchantsFlActivity.class).putExtra("canteenId",dataBean.getId()+"").putExtra("schoolid",SPUtil.get(getContext(),IntentKey.ADDRESSID,"")).putExtra("flid","0").putExtra("title",dataBean.getName()));
                                }
                            }
                        });

                            if (system_id.equals("1")){
                                baseViewHolder.setVisible(R.id.rl_status,true);
                                baseViewHolder.setImageResource(R.id.img_type,R.mipmap.icon_coupon_shiyong);
                            }
                            else if (system_id.equals("0")){
                                baseViewHolder.setVisible(R.id.rl_status,false);

                            }
                            else if (system_id.equals("2")){
                                baseViewHolder.setVisible(R.id.rl_status,true);
                                baseViewHolder.setImageResource(R.id.img_type,R.mipmap.icon_coupon_shixiao);
                            }

                        }


                    };


                    View emptyview = View.inflate(getContext(), R.layout.empty_emptylayout, null);

                    adapter.setEmptyView(emptyview);
                    recyclerView.setAdapter(adapter);

                }

            }
            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }

}
